package com.first.controller;


import com.first.dao.impl.UrlDaoImpl;
import com.first.entity.BType;
import com.first.entity.Book;
import com.first.service.BookService;
import com.first.service.OrderService;
import com.first.util.DBUtil;
import com.first.util.LayTab;
import com.first.util.Onload;
import com.first.util.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookController {

    @RequestMapping("/aaa")
    public  LayTab  aaa(){
        String sql = " select * from user";
        List<Map> maps = DBUtil.queryListMap(sql);
        LayTab layTab = new LayTab(maps.size(), maps);
        System.out.println(layTab);
        return  layTab;
    }

    @Resource
    private BookService books;

    @Resource
    private OrderService orders;
    Result result = new Result();

    private ArrayList<String> urls;


    @RequestMapping("/addType")
    public  Result  addType(@RequestBody BType bType){
        Result result = new Result();
        int i = books.addType(bType);
        if(i>0){
            result.setCode(1);
            result.setMsg("新增成功");
        }else {
            result.setCode(0);
            result.setMsg("新增失败");
        }
        return result;
    }


    @RequestMapping("/queryBookByTypeid")
    public LayTab queryBookByTypeid(Book book){
        List<Map> maps = books.queryBookByTypeid(book);
        LayTab layTab = new LayTab();
        if(maps !=null){
            layTab.setCount(maps.size());
            layTab.setData(maps);
        }
       return layTab;

    }



    @RequestMapping("/getAllBookType")
    public  LayTab queryAllBookType(Book book){
        List<Map> maps = books.queryAllBookType(book);
        LayTab layTab = new LayTab(maps.size(), maps);
        return layTab;

    }

    /**
     * 书籍信息查询/书名模糊查询
     * @param book
     * @return
     */
    @RequestMapping("/query")
    public LayTab queryBook( Book book){
        List<Map> list = books.queryBooks(book);
        LayTab layTab = new LayTab();
        if (list !=null){
            layTab.setCount(books.getBookCount(book));
            layTab.setData(list);
        }
        return layTab;
    }
    /**
     * 获取上传的图片路径
     * @param files
     */
    @RequestMapping("img")
    public void getUrl(@RequestParam("file") MultipartFile[] files){
        urls = Onload.getURLS(files);

    }
    /**
     * 书籍信息的添加与修改
     * @param book
     * @return
     */
    @RequestMapping("/update")
    public Result updateBook ( @RequestBody Book book){
        Result result = new Result();
        UrlDaoImpl urlDao = new UrlDaoImpl();
        if(book.getBookid()==0){
            int i = books.addBook(book);
            if (i<1){
                result.setCode(0);
                result.setMsg("添加失败");
            }else{
                result.setCode(1);
                result.setMsg("添加成功");
                urlDao.addImgUlr(urls,i);
            }

        }else {
            int i = books.updateBookById(book);
            if(i>0){
                result.setCode(1);
                result.setMsg("修改成功");
                urlDao.addImgUlr(urls,i);
            }else{
                result.setCode(0);
                result.setMsg("修改失败");
            }
        }
        return result;
    }

    /**
     * 书籍删除通过id
     * @param bookid
     * @return
     */

    @RequestMapping("/del")
    public Result delBook(Integer bookid){
        int i = books.delBookById(bookid);
        if(i>0){
            result.setCode(1);
            result.setMsg("删除成功");
        }else{
            result.setCode(0);
            result.setMsg("删除失败");
        }
        return result;
    }


        /**
         * 获取书籍类型
         * @return
         */
    @RequestMapping("/type")
    public List getBookType(){
        return books.getBookType();
    }
}
